/*
 *Author: Casey M. Wylie
 *Date: January 25, 2010
 *Filename: TestArrayStack.java
 *Class: ITCS 2214 Data Structures
 */


import jss2.*;
import java.util.*;

public class TestArrayStack 
{
    public static void main(String[] args) 
    {
	Scanner in = new Scanner(System.in);
	ArrayStack as = new ArrayStack();
	String word;
	String options = "TestArrayStack 1.0 (http://dfoyer.com)\nUsage: java TestArrayStack [options]\n\nOptions:\n  -push <elements>\tPush elements on stack.\n  -pop\t\t\tReturns and removes element on top of the stack.\n  -peek\t\t\tReturns top element on the stack.\n  -isEmpty\t\tReturns true if stack is empty.\n  -size\t\t\tReturns number of elements in the stack.\n  -toString\t\tPrints a string representation of the stack.\n  -help\t\t\tDisplay this message.\n  -quit\t\t\tExit program.\n";
	
	System.out.println(options);

	while(true) 
        {
	    
	    System.out.print("> ");
	    word = in.nextLine();
	    String[] tokens = word.split(" ");

	    if(tokens[0].equals("push"))
	    {
		if(tokens.length<2) 
		{
		    System.out.println("Error: Missing arguments.\nUsage: push <args>\n");
		    continue;
		}
	        for(int i = 1; i < tokens.length;i++)
		    as.push(tokens[i]);
	    
	    }
	      
      
	    else if(tokens[0].equals("pop"))
	    {
		try
		{
		    System.out.println(as.pop());
		}
		catch(Exception e)
		{
		    System.out.println(e.getMessage()+"\n");
		    continue;
		}
	    }
	    
	    else if(tokens[0].equals("peek"))
	    {
		try
		{
		    System.out.println(as.peek());
		}
		catch(Exception e)
		{
		    System.out.println(e.getMessage()+"\n");
		    continue;
		}
	    }

	    else if(tokens[0].equals("isEmpty"))
	    {
		System.out.println(as.isEmpty());
	    }

	    else if(tokens[0].equals("size"))
	    {
		System.out.println(as.size());
	    }
	    
	    else if(tokens[0].equals("toString"))
	    {
		System.out.println(as.toString());
	    }
	    
	    else if(tokens[0].equals("help"))
	    {
		System.out.println(options);
	    }

	    else if(tokens[0].equals("quit"))
	    {
		break;
	    }
	    
	    else 
	    {
		System.out.println("Error: Function call to "+tokens[0]+" is invalid.");
	    }
	   
	    
	    
	       
	    System.out.println();   
	}

    }
}